package com.ibeetl.cms.service;

import com.ibeetl.admin.console.dao.DictConsoleDao;
import com.ibeetl.admin.console.web.dto.EnterpriseExcelImportData;
import com.ibeetl.admin.core.service.BaseService;
import com.ibeetl.admin.core.service.CoreUserService;
import com.ibeetl.admin.core.util.PlatformException;
import com.ibeetl.cms.dao.EnterpriseDao;
import com.ibeetl.cms.entity.Enterprise;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Enterprise Service
 */

@Service
@Transactional
public class EnterpriseService extends BaseService<Enterprise>{

    @Autowired
    private EnterpriseDao enterpriseDao;

    @Autowired
    private CoreUserService coreUserService;

    @Autowired
    private DictConsoleDao dictConsoleDao;

    public PageQuery<Enterprise>queryByCondition(PageQuery query){
        PageQuery ret =  enterpriseDao.queryByCondition(query);
        queryListAfter(ret.getList());
        return ret;
    }

    public PageQuery<Enterprise>queryByConditionInfo(PageQuery query){
        PageQuery ret =  enterpriseDao.queryByConditionInfo(query);
        queryListAfter(ret.getList());
        return ret;
    }

    public PageQuery<Enterprise>queryByConditionMaintain(PageQuery query){
        PageQuery ret =  enterpriseDao.queryByConditionMaintain(query);
        queryListAfter(ret.getList());
        return ret;
    }

    public void batchDelEnterprise(List<Long> ids){
        try {
            enterpriseDao.batchDelEnterpriseByIds(ids);
        } catch (Exception e) {
            throw new PlatformException("批量删除Enterprise失败", e);
        }
    }
    public Enterprise findCompanyId(long userid){
        Enterprise enterprise= enterpriseDao.findCompanyId(userid);
        return enterprise;
    }
    public Enterprise findenterprise(long userid){
        Enterprise enterprise= enterpriseDao.findenterprise(userid);
        return enterprise;
    }

    public void invalid(List<Long> ids){
        try {
            enterpriseDao.invalid(ids);
        } catch (Exception e) {
            throw new PlatformException("批量失效Enterprise失败", e);
        }
    }

    public void batchInsert(List<EnterpriseExcelImportData> list) {
        final Map<Integer, EnterpriseExcelImportData> map = new HashMap<>();
        List<Enterprise> updateList = new ArrayList<Enterprise>();
        //逐个按照顺序导入
        for (EnterpriseExcelImportData item : list) {
            Enterprise dict = new Enterprise();
            dict.setEnterpriseName(item.getEnterpriseName());
            dict.setOrganizationCode(item.getOrganizationCode());
            dict.setBusinessQuota(item.getBusinessQuota());
            dict.setDirectoryEnterprises(dictConsoleDao.findType(item.getDirectoryEnterprises()));
            dict.setEnterpriseDirectory(item.getEnterpriseDirectory());
            Enterprise enterprise = enterpriseDao.selectByOrganizationCodeInfo(item.getOrganizationCode(), item.getEnterpriseDirectory());
            if (enterprise == null) {
                enterpriseDao.insertInfo(dict);
            } else {
               enterpriseDao.updateByCodeInfo(dict);
                /*
                 ** 功能描述:
                 *  1.同步更新企业基本信息表 年度、企业类型、名额字段信息
                 *  2.登录用户名同步更新
                 ** @author liujun
                 ** @date 2019/12/24 16:22
                 **/
                Integer userId = enterpriseDao.findorgUserId(item.getOrganizationCode());
               if(userId != null){
                   coreUserService.updateUserName(userId,item.getEnterpriseName());
               }
               enterpriseDao.updateByCode(dict);
            }
        }
    }

    //根据组织机构获取企业id
    public Integer findorgUserId(String organizationCode){
        return enterpriseDao.findorgUserId(organizationCode);
    }

    //根据组织机构获取id
    public Integer findorgId(String organizationCode){
        return enterpriseDao.findorgId(organizationCode);
    }

    //根据user_id查询
    public Enterprise queryByUserId(Integer userId){
        return enterpriseDao.queryByUserId(userId);
    }

    public List<Enterprise> findAll(){//查询所有企业
        return enterpriseDao.findAll();
    }

    public List<Enterprise> findAllOrg(){//查询所有企业基本信息的组织机构
        return enterpriseDao.findAllOrg();
    }

    public List<Enterprise> findEnterpriseInfo(){//查询所有企业基本信息的组织机构
        return enterpriseDao.findEnterpriseInfo();
    }

    public int updateQuota(Integer id){
        return enterpriseDao.updateQuota(id);
    }

    /**
     * 批量修改用户userId <br/>
     * 删除企业用户后，企业基本信息变为无主
     *
     * @param
     *
     */
    public void batchUpdate(List<Long> dels) {
        try {
            enterpriseDao.batchUpdate(dels);
        } catch (Exception e) {
            //throw new PlatformException("批量删除用户失败", e);
        }
    }

    /**
     * 批量删除用户userId <br/>
     *
     * @param
     *
     */
    public void deleteEnterprise(List<Long> dels) {
        try {
            enterpriseDao.deleteEnterprise(dels);
        } catch (Exception e) {
            //throw new PlatformException("批量删除用户失败", e);
        }
    }

}